package com.qf.vue2.web.controller;

import com.qf.vue2.pojo.R;
import com.qf.vue2.pojo.User;
import com.qf.vue2.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public R login(@RequestBody User user, HttpServletRequest req){

        //通过service完成登录
        User loginUser = userService.login(user);
        if (loginUser == null){
            //登陆失败
            return R.fail("用户名或密码错误");
        }

        //登录成功
        req.getSession().setAttribute("user",loginUser);

        return R.success("登录成功");
    }
}
